﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Resolution;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Blueprints.Defaults
{
    /// <summary>
    /// Describes a KeyValuePair.
    /// </summary>
    internal sealed class DefaultKeyValuePairDescriptor : IObjectDescriptor
    {
        /// <summary>
        /// Creates a new DefaultKeyValuePairDescriptor.
        /// </summary>
        /// <param name="type">The generic KeyValuePair type.</param>
        public DefaultKeyValuePairDescriptor(Type type)
        {
            Type = type;
            Type[] arguments = type.GetTypeInfo().GenericTypeParameters;
            List<IPropertyDescriptor> requiredProperties = new List<IPropertyDescriptor>();
            requiredProperties.Add(new ReflectionPropertyDescriptor(GetProperty(type, "Key")));
            requiredProperties.Add(new ReflectionPropertyDescriptor(GetProperty(type, "Value")));
            RequiredProperties = requiredProperties;
            OptionalProperties = new IPropertyDescriptor[0];
        }

        /// <summary>
        /// Gets the required properties of the object.
        /// </summary>
        /// <returns>An ordered list of required properties to supply to the constructor of the object in that order.</returns>
        public IReadOnlyList<IPropertyDescriptor> RequiredProperties
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the optional properties of the object.
        /// </summary>
        /// <returns>A list of optional properties.</returns>
        public IReadOnlyList<IPropertyDescriptor> OptionalProperties
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the type this descriptor describes.
        /// </summary>
        public Type Type
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates an instance of the class.
        /// </summary>
        /// <param name="parameters">The values of the required properties, in the order returned by the GetRequiredProperties() method.</param>
        /// <returns>An instance of the class.</returns>
        public object CreateInstance(object[] parameters)
        {
            return Activator.CreateInstance(Type, parameters);
        }

        private static PropertyInfo GetProperty(Type type, string name)
        {
            PropertyInfo property = type.GetRuntimeProperty(name);
            if (property == null
		|| property.GetMethod == null
                || !property.GetMethod.IsPublic
                || property.GetMethod.IsStatic)
            {
                throw new BlueprintException("KeyValuePair<,> should have a '" + name + "' property, but it could not be found");
            }

            return property;
        }
    }
}